package com.springapp.mvc.dao;

import com.springapp.mvc.model.ApplyOrder;
import com.springapp.mvc.model.EffectOrder;
import com.springapp.mvc.model.Picture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * User: yzh
 * Date: 11/6/15
 * Time: 01:52
 */
public class OrderDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<ApplyOrder> getApply(String type) {
        String sql = "select * from  apply_order where status = ?;";
        List<ApplyOrder> result = jdbcTemplate.query(sql, new Object[]{type}, new BeanPropertyRowMapper(ApplyOrder.class));
        return result;
    }

    public List<ApplyOrder> chgStatus(String status) {
        return null;
    }

    public ApplyOrder getApplyById(String id) {
        String sql = "select * from  apply_order where order_id = ?;";
        List<ApplyOrder> result = jdbcTemplate.query(sql, new Object[]{id}, new BeanPropertyRowMapper(ApplyOrder.class));
        if (null != result && result.size() > 0) {
            return result.get(0);
        }
        return null;
    }

    public List<Picture> getApplyPicture(String id) {
        String sql = "select * from picture where picture_id in (select picture from apply_order where order_id = ?);";
        List<Picture> result = jdbcTemplate.query(sql, new Object[]{id}, new BeanPropertyRowMapper(Picture.class));
        return result;
    }

    public void updateStatus(String id, String status) {
        String sql = "update apply_order set status = ? where order_id = ?;";
        jdbcTemplate.update(sql, new Object[]{status, id});
    }

    public List<EffectOrder> getEffectOrder(String type) {
        String sql = "select * from effect_order where status = ?;";
        List<EffectOrder> result = jdbcTemplate.query(sql, new Object[]{type}, new BeanPropertyRowMapper(EffectOrder.class));
        return result;
    }

    public EffectOrder getEffectOrderById(int effectId) {
        String sql = "select * from  effect_order a where a.effect_order = ?;";
        List<EffectOrder> result = jdbcTemplate.query(sql, new Object[]{effectId}, new BeanPropertyRowMapper(EffectOrder.class));
        if (null != result && result.size() > 0) {
            return result.get(0);
        }
        return null;
    }

    public void addToEffect(ApplyOrder applyOrder) {

    }

    public void updateEffect(EffectOrder order) {
    }

    public ApplyOrder publish(String id) {
        return null;
    }

    public void addApply(ApplyOrder order) {

    }
}
